package baobaotao.controller;

import baobaotao.pojo.LoginCommand;
import baobaotao.pojo.User;
import baobaotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2015/4/9.
 */
@Controller
@RequestMapping("/user2")
public class UserController2 {

    @Autowired
    private UserService userService;

     @RequestMapping("/login")
    public String loginPage(){
//        model.addAttribute("admin", admin);
        return "baobaotao/login";
    }

    @RequestMapping("/loginCheck")
    public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) {
        boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
        if (!isValidUser) {
            return new ModelAndView("baobaotao/login", "error", "用户名或密码错误!");
        }else {
            User user = userService.findUserByUserName(loginCommand.getUserName());
            user.setLastIp(request.getRemoteAddr());
            user.setLastVisit(new Date());
            userService.loginSuccess(user);
            request.getSession().setAttribute("user", user);
            return new ModelAndView("baobaotao/main");
        }
    }
}
